package com.eedi.framework.protocol.service;

import com.eedi.framework.common.enums.CommonStatusEnum;
import com.eedi.framework.common.pojo.PageResult;
import com.eedi.framework.common.util.object.BeanUtils;
import com.eedi.framework.protocol.controller.sys.vo.SysProtocolPageReq;
import com.eedi.framework.protocol.controller.sys.vo.SysProtocolSaveReq;
import com.eedi.framework.protocol.dal.dataobject.ProtocolDO;
import com.eedi.framework.protocol.dal.dataobject.ProtocolHistoryDO;
import com.eedi.framework.protocol.dal.mapper.ProtocolHistoryMapper;
import com.eedi.framework.protocol.dal.mapper.ProtocolMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static com.eedi.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.eedi.framework.protocol.enums.SysProtocolErrorCodeConstants.SYS_PROTOCOL_NOT_EXISTS;

/**
 * 协议配置 Service 实现类
 *
 * @author 永聚长青源码
 */
@Service
@Validated
public class SysProtocolServiceImpl implements SysProtocolService {

    @Resource
    private ProtocolMapper protocolMapper;

    @Resource
    private ProtocolHistoryMapper protocolHistoryMapper;

    @Override
    public String createProtocol(SysProtocolSaveReq createReqVO) {
        // 插入
        ProtocolDO protocol = BeanUtils.toBean(createReqVO, ProtocolDO.class);
        protocolMapper.insert(protocol);

        // 协议版本生成
        ProtocolHistoryDO historyProtocol = BeanUtils.toBean(protocol, ProtocolHistoryDO.class);
        historyProtocol.setDeleted(null);
        historyProtocol.setSysProtocolId(null);
        protocolHistoryMapper.insert(historyProtocol);
        return protocol.getSysProtocolId();
    }

    @Override
    public void updateProtocol(SysProtocolSaveReq updateReqVO) {
        // 校验存在
        ProtocolDO existProtocol = validateProtocolExists(updateReqVO.getSystemProtocolId());
        // 更新
        ProtocolDO updateObj = BeanUtils.toBean(updateReqVO, ProtocolDO.class);
        if (!existProtocol.getSysProtocolContent().equals(updateReqVO.getProtocolContent()) ||
                !existProtocol.getSysProtocolApplyType().equals(updateReqVO.getProtocolApplyType()) ||
                !existProtocol.getSysProtocolName().equals(updateReqVO.getName())) {
            ProtocolHistoryDO historyProtocol = BeanUtils.toBean(updateReqVO, ProtocolHistoryDO.class);
            protocolHistoryMapper.insert(historyProtocol);
        }
        protocolMapper.updateById(updateObj);
    }

    @Override
    public void deleteProtocol(String systemProtocolId) {
        // 校验存在
        validateProtocolExists(systemProtocolId);
        // 删除
        protocolMapper.deleteById(systemProtocolId);
    }

    private ProtocolDO validateProtocolExists(String systemProtocolId) {
        ProtocolDO protocolDO = protocolMapper.selectById(systemProtocolId);
        if (protocolDO == null) {
            throw exception(SYS_PROTOCOL_NOT_EXISTS);
        }
        return protocolDO;
    }

    @Override
    public ProtocolDO getProtocol(String systemProtocolId) {
        return protocolMapper.selectById(systemProtocolId);
    }

    @Override
    public void updateProtocolStatus(String systemProtocolId, CommonStatusEnum status) {
        // 校验存在
        validateProtocolExists(systemProtocolId);

        // 更新状态
        ProtocolDO updateObj = new ProtocolDO();
        updateObj.setSysProtocolId(systemProtocolId);
        updateObj.setSysProtocolPublishStatus(status);
        protocolMapper.updateById(updateObj);
    }

    @Override
    public PageResult<ProtocolDO> getProtocolPage(SysProtocolPageReq pageReqVO) {
        return protocolMapper.selectPage(pageReqVO);
    }

}
